<?php

namespace app\logics;

use app\collections\Collection;
use app\libs\Application;
use app\libs\Sms;
use app\tools\Table;
use app\traits\ErrorCode;
use Phalcon\Di\Injectable;

class Common extends Injectable
{
    use ErrorCode;

    /**
     * @param $mobile
     * @param $captcha
     * @param int $type
     * @return bool
     */
    public static function verifyCaptcha($mobile, $captcha, $type = 0)
    {
        $key = 'captcha_' . $mobile;
        $redis = Application::$app->getCache();
        $data = $redis->get($key);
        if (!$data || !isset($data['captcha'])) {
            self::$errorCode = ERROR_CAPTCHA_NOT_FOUND;
            return false;
        }
        if ($data['type'] != $type) {
            self::$errorCode = ERROR_OPERATION_FAILED;
            return false;
        }
        if ($data['captcha'] == $captcha) {
            return true;
        }
        self::$errorCode = ERROR_CAPTCHA_VERIFY_FAILED;
        return false;
    }

    /**
     * 发送验证码给手机号
     * @param $mobile
     * @param $captcha
     * @param $type int 操作类型 0表示注册 1表示找回密码
     * @param int $captchaExpire
     * @param int $sendExpire 发送过后隔多长时间才能重新发 单位秒
     */
    public static function sendCaptchaToMobile($mobile, $ip, $type = 0, $cacheExpire = 60, $sendExpire = 60)
    {
        if (!static::verifyKenyaMobile($mobile)) {
            self::$errorCode = ERROR_MOBILE_NUMBER_INVALID;
            return false;
        }
        $key = 'captcha_' . $mobile;
        $ipKey = 'captcha_' . $ip;
        $redis = Application::$app->getCache();
        $data = $redis->get($key);
        $ipSendCount = (int)$redis->get($ipKey);
        if ($ipSendCount > 100) {
            self::$errorCode = ERROR_REQUEST_TOO_BUSY;
            return false;
        }
        ++$ipSendCount;
        $redis->save($key, $ipSendCount, $cacheExpire);
        if ($data) {
            //判断当前手机号是否在限定的时间内发过短信
            $time = time() + $sendExpire;
            if (isset($data['sendExpire']) && $data['sendExpire'] > $time) {
                self::$errorCode = ERROR_CAPTCHA_IS_SEND;
                return false;
            }
        }
        $captcha = Application::$app->getRandNumber(6); //获得6位数的数字作为验证码
        $sms = new Sms();
        $message = 'Your Captcha is ' . $captcha;
        if (!$sms->sendMsg($mobile, $message)) {
            self::$errorCode = ERROR_SEND_SMS_FAILED;
            Application::$app->logger->error($sms->errorMsg);
            return false;
        }
        $data = [
            'captcha' => $captcha,
            'type' => $type,
            'sendExpire' => time() + $sendExpire
        ];
        //记录发送短信日志
        $collection = new Collection(Table::MONGO_SMS_LOG);
        $collection->insertOne([
            'mobile' => $mobile,
            'ip' => $ip,
            'message' => $message
        ]);
        $redis->save($key, $data, $cacheExpire);
        return $data;
    }

    public static function verifyKenyaMobile($mobile)
    {
        $mobilePreg = '#^07[0-9]{8}$#';
        if (!preg_match($mobilePreg, $mobile)) {
            return false;
        }
        return true;
    }
}